package io.jboot.fly.service.impl;

import com.jfinal.plugin.activerecord.Db;
import io.jboot.aop.annotation.Bean;
import io.jboot.db.model.Columns;
import io.jboot.fly.service.UserActionService;
import io.jboot.fly.model.UserAction;
import io.jboot.service.JbootServiceBase;

import javax.inject.Singleton;
import java.util.Date;
import java.util.List;

@Bean
@Singleton
public class UserActionServiceImpl extends JbootServiceBase<UserAction> implements UserActionService {

    @Override
    public List<UserAction> findByUserId(String userId, Date date) {
        Columns columns = Columns.create("user_id", userId)
                .ge("created", date);

        return DAO.findListByColumns(columns, "created desc");
    }

    @Override
    public List<UserAction> findByUserId(String userId, int count) {
        Columns columns = Columns.create("user_id", userId);
        return DAO.findListByColumns(columns, "created desc", count);
    }

    @Override
    public List<UserAction> findByUserIdAndAction(String userId, String action, Date date) {
        Columns columns = Columns.create("user_id", userId)
                .eq("action", action)
                .ge("created", date);

        return DAO.findListByColumns(columns, "created desc");
    }

    @Override
    public List<UserAction> findByUserIdCommentIdAction(String userId, String commentId, String action) {
        Columns columns = Columns.create("user_id", userId)
                .eq("comment_id", commentId)
                .eq("action", action);
        return DAO.findListByColumns(columns, "created desc");
    }

    @Override
    public List<UserAction> findByUserIdPostIdAction(String userId, String postId, String action) {
        Columns columns = Columns.create("user_id", userId)
                .eq("post_id", postId)
                .eq("action", action);
        return DAO.findListByColumns(columns, "created desc");
    }

    @Override
    public boolean deleteByUserIdCommentIdAction(String userId, String commentId, String action) {
        String sql = "delete from user_action where user_id = ? and comment_id = ? and action = ?";
        return Db.update(sql, userId, commentId, action) > 0;
    }

    @Override
    public boolean deleteByUserIdPostIdAction(String userId, String postId, String action) {
        String sql = "delete from user_action where user_id = ? and post_id = ? and action = ?";
        return Db.update(sql, userId, postId, action) > 0;
    }
    @Override
    public List<UserAction> findByUserIdAndAction(String userId,String action){
        Columns columns = Columns.create("user_id", userId)
                .eq("action", action);
        return DAO.findListByColumns(columns, "created desc");
    }
    public List<UserAction> findByPostAndAction(String postId,String action){
        Columns columns = Columns.create("post_id", postId)
                .eq("action", action);
        return DAO.findListByColumns(columns, "created desc");
    }

}